package com.baizhou.honglv.controller;

import com.baizhou.honglv.bean.exception.SysException;
import com.baizhou.honglv.bean.wrapper.ResultWrapper;
import com.baizhou.honglv.service.BackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author HaiPeng Wang
 * @date 2021/7/17 12:46
 * @Description:
 */
@Api(value = "BackController", tags = {"后台管理API"})
@Controller
@RequestMapping("/back")
public class BackController extends BaseController {

    @Autowired
    BackService backService;

    @ResponseBody
    @ApiOperation(value = "上传文件",notes = "上传编写好的Excel文件")
    @ApiImplicitParams({
    })
    @PostMapping(value = "/upload")                                      //BindingResult 是一种校验方式
    public ResultWrapper upload( @RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) {   //name uCode
//            参数校验
//        validateParams(bindingResult)

        String uploadPath = request.getServletContext().getRealPath("upload");
        try {
            backService.uploadFile(file,uploadPath,request.getInputStream());
        }catch (IOException e){
            e.printStackTrace();
            throw new SysException("上传失败！");
        }
        return ResultWrapper.success("上传成功");
    }


}
